package com.sqk.reggie.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.FileNotFoundException;


/**
 * 全局异常处理器
 */
@Slf4j
@ResponseBody
//@ControllerAdvice(annotations = {Controller.class, RestController.class})
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(RuntimeException.class)
    public Result<String> sqlExceptionHandler(RuntimeException runtimeException){
        log.info("异常信息 {}",runtimeException.getMessage());
            if (runtimeException.getMessage().contains("Duplicate entry")){
                return Result.error("用户名已存在!");
        }
        return Result.error("未知错误!");
    }

    @ExceptionHandler(FileNotFoundException.class)
    public Result<String> exception(FileNotFoundException fileNotFoundException){
        log.info("异常信息： {}", fileNotFoundException.getMessage());
        return Result.error("文件资源错误!");
    }
}
